package resbuilder

import "fmt"

//Raw Results struct
type TestRawCapture struct {
	Start      string
	RunString  string
	TestName   string
	Middle     string
	PassFail   string
	TimeTook   string
	FailString string
	Passed     bool
}

//Raw Results manipulator
func ResultBuilder(raw []byte) []TestRawCapture {

	var results = []TestRawCapture{}
	stringcount := 0

	for i := 0; i < len(raw); {

		if raw[i] == '=' && stringcount == 0 {
			results = append(results, TestRawCapture{"", "", "", "", "", "", "", false})
			results[len(results)-1].Start = captureStart(raw, &i)
			stringcount++
		} else if raw[i] == 'R' && stringcount == 1 {
			results[len(results)-1].RunString = captureRUN(raw, &i)
			stringcount++
		} else if raw[i] >= 64 && raw[i] <= 122 && stringcount == 2 {
			results[len(results)-1].TestName = captureTestName(raw, &i)
			stringcount++
		} else if raw[i] == '-' && stringcount == 3 {
			results[len(results)-1].Middle = captureMiddle(raw, &i)
			stringcount++
		} else if raw[i] >= 64 && raw[i] <= 122 && stringcount == 4 {
			results[len(results)-1].PassFail = capturePassFail(raw, &i)
			stringcount++
		} else if raw[i] == '(' && stringcount == 5 {
			results[len(results)-1].TimeTook = captureTimeTook(raw, &i)
			stringcount++
			if results[len(results)-1].PassFail == "PASS" {
				stringcount = 0
				results[len(results)-1].Passed = true
			}

		} else if raw[i] >= 64 && raw[i] <= 122 && stringcount == 6 && results[len(results)-1].PassFail != "PASS" {
			results[len(results)-1].FailString = captureFailString(raw, &i)
			stringcount = 0
		} else {
			i++
		}
	}

	return results
}

func captureStart(raw []byte, i *int) string {

	for raw[*i] == '=' {
		*i++
	}

	return "==="
}

func captureRUN(raw []byte, i *int) string {

	temp := ""

	for raw[*i] != ' ' {
		temp += string(raw[*i])
		*i++
	}

	return temp
}

func captureTestName(raw []byte, i *int) string {

	temp := ""

	for raw[*i] != ' ' && raw[*i] != '\n' {
		temp += string(raw[*i])
		*i++
	}

	return temp
}

func captureMiddle(raw []byte, i *int) string {

	for raw[*i] == '-' {
		*i++
	}

	return "---"
}

func capturePassFail(raw []byte, i *int) string {
	temp := ""

	for raw[*i] != ':' && raw[*i] != '\n' {
		temp += string(raw[*i])
		*i++
	}

	return temp

}

func captureTimeTook(raw []byte, i *int) string {

	temp := ""

	if raw[*i] == '(' {
		*i++
		for raw[*i] != ')' {
			temp = temp + string(raw[*i])
			*i++
		}
	}
	return temp
}

func captureFailString(raw []byte, i *int) string {

	temp := ""
	for raw[*i] != ' ' {
		temp = temp + string(raw[*i])
		*i++
	}

	return temp
}

func ResultPrinter(results []TestRawCapture) {

	for i := 0; i < len(results); i++ {
		fmt.Println(results[i].Start, results[i].RunString, results[i].TestName)
		fmt.Println(results[i].Middle, results[i].PassFail, results[i].TestName, results[i].TimeTook)
		fmt.Println(results[i].FailString, results[i].Passed)
	}
}

func ResPrint(results []TestRawCapture) bool {
	temp := true
	tempstring := ""
	//TO DO: add time_amount here + implement

	for i := 0; i < len(results); i++ {
		fmt.Println(results[i].TestName, "\t\t", results[i].PassFail)
		temp = temp && results[i].Passed
	}

	if temp {
		tempstring = "PASS"
	} else {
		tempstring = "FAIL"
	}

	fmt.Println("Result: ", tempstring)
	return temp
}

func FinalRes(results []TestRawCapture) bool {
	temp := true
	//tempstring := ""
	//TO DO: add time_amount here + implement

	for i := 0; i < len(results); i++ {
		temp = temp && results[i].Passed
	}

	// if temp {
	// 	tempstring = "PASS"
	// } else {
	// 	tempstring = "FAIL"
	// }

	//fmt.Println("Result: ", tempstring)
	return temp
}
